/******************************************************
// Copyright 2008 Derrick Pallas. All rights reserved.
//
// This file is part of c-ptr.
//
// c-ptr is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// c-ptr is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with c-ptr.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LOCKING_PTR_H
#define LOCKING_PTR_H

#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>

namespace ptr
{
  template < typename, class > class locked;
  template < typename T, class M = boost::mutex >
    class locking
    {
    public:
      typedef T my_type;
      typedef M mutex_type;

    private:
      mutex_type & mutex;
      boost::shared_ptr<my_type> ptr;

    public:
      template < typename FROM >
      locking( FROM that, mutex_type & m )
        : ptr(that), mutex(m) { }

      locked<my_type,mutex_type>
      lock() const
        { return locked<my_type,mutex_type>( ptr, mutex ); }
    };
}

#endif // LOCKING_PTR_H
